package br.com.bancodecurriculos.util.dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import br.com.bancodecurriculos.util.properties.PropertiesFactory;

public class JDBCConnectionFactory {

	private static JDBCConnectionFactory instance;

	private static final String CONFIG_FILE = "../dao/jdbc_config.properties";

	private static final String DB = "mysql";

	private static final String DRIVER = DB + ".driver";
	private static final String URL = DB + ".url";
	private static final String USER = DB + ".user";
	private static final String PASSWORD = DB + ".password";

	private JDBCConnectionFactory() {

	}

	public synchronized static JDBCConnectionFactory getInstance()
			throws IOException {
		if (instance == null) {
			instance = new JDBCConnectionFactory();
		}
		return instance;
	}

	public Connection getConnection() throws ClassNotFoundException,
			SQLException, IOException {
		Class.forName(PropertiesFactory.getInstance().getProperty(CONFIG_FILE,
				DRIVER));
		return DriverManager.getConnection(PropertiesFactory.getInstance()
				.getProperty(CONFIG_FILE, URL), PropertiesFactory.getInstance()
				.getProperty(CONFIG_FILE, USER), PropertiesFactory
				.getInstance().getProperty(CONFIG_FILE, PASSWORD));
	}

}
